package com.sanji.sjmallmodule.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigAPI;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.http.NoHttpCallBack;
import com.sanji.sjmallmanager.http.NoHttpListener;
import com.sanji.sjmallmodule.account.MyScoreActivity;
import com.sanji.sjmallmodule.account.MyWalletActivity;
import com.sanji.sjmallmodule.account.PersonCenterActivity;
import com.sanji.sjmallmodule.account.SaleAfterActivity;
import com.sanji.sjmallutils.SharePreferenceUtil;
import com.sanji.sjmallutils.Tools;
import com.umeng.analytics.MobclickAgent;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;
import static com.sanji.sjmallapp.MallApplication.myShare;

/**
 * 我的
 */
public class MyFragment extends Fragment implements View.OnClickListener {
    Intent intent = null;
    @BindView(R.id.all_order_layout)
    RelativeLayout allOrderLayout;
    @BindView(R.id.obligation_relative)
    RelativeLayout obligationRelative;
    @BindView(R.id.shipped_relative)
    RelativeLayout shippedRelative;
    @BindView(R.id.account_relative)
    RelativeLayout accountRelative;
    @BindView(R.id.my_wallet_arrow_relative)
    RelativeLayout walletArrowRelative;
    @BindView(R.id.my_after_sale_arrow_relative)
    RelativeLayout afterSaleArrowRelative;
    @BindView(R.id.my_score_arrow_relative)
    RelativeLayout myScoreArrowRelative;
    @BindView(R.id.person_info_arrow)
    TextView personInfoArrow;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.unPaid_img)
    ImageView unPaidImg;
    @BindView(R.id.unShip_img)
    ImageView unShipImg;
    private View rootView;
    private SharePreferenceUtil sp;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_my, null);
            ButterKnife.bind(this, rootView);
            initView();
        }
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    public void initView() {
        sp = myShare;
        userName.setText(sp.getString(ConfigConts.TRUE_NAME, null));
        walletArrowRelative.setOnClickListener(this);
        afterSaleArrowRelative.setOnClickListener(this);
        myScoreArrowRelative.setOnClickListener(this);
        personInfoArrow.setOnClickListener(this);
        allOrderLayout.setOnClickListener(this);
        obligationRelative.setOnClickListener(this);
        shippedRelative.setOnClickListener(this);
        accountRelative.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_score_arrow_relative:
                //我的积分
                intent = new Intent(getActivity(), MyScoreActivity.class);
                startActivity(intent);
                break;
            case R.id.my_after_sale_arrow_relative:
                //我的售后
                intent = new Intent(getActivity(), SaleAfterActivity.class);
                startActivity(intent);
                break;
            case R.id.my_wallet_arrow_relative:
                //我的钱包
                intent = new Intent(getActivity(), MyWalletActivity.class);
                startActivity(intent);
                break;
            case R.id.person_info_arrow:
                //个人中心
                intent = new Intent(getActivity(), PersonCenterActivity.class);
                startActivity(intent);
                break;
            case R.id.all_order_layout:
                // 全部订单
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("pageIndex", 0);
                startActivity(intent);
                break;
            case R.id.obligation_relative:
                //待付款
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("pageIndex", 1);
                startActivityForResult(intent, 1);
                break;
            case R.id.shipped_relative:
                //待发货
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("pageIndex", 2);

                startActivityForResult(intent, 2);
                break;
            case R.id.account_relative:
                //已付款
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("pageIndex", 3);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MyFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MyFragment");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    unPaidImg.setImageResource(R.mipmap.no_obligation);
                }
                break;
            case 2:
                if (resultCode == RESULT_OK) {
                    unShipImg.setImageResource(R.mipmap.no_shipped);
                }
                break;
        }

    }
}
